from sys import exit
TM_START = 0x185a1
TM_SIZE  = 256
GF_START = 0X176100

def readWord(f):
    return ord(f.read(1)) | (ord(f.read(1)) << 8)

try:
    rom = open("BoFII-EN.smc", "rb")
except IOError:
    print "<!> Cannot open rom file;"
    exit(-1)
else:
    print "<i> Extracion begin;"
###---
rom.seek(GF_START)
tiles = list()
for x in xrange(TM_SIZE):
    tiles.append(rom.read(16)) #read 1 tile each time

print "<i> %i tiles read;" % len(tiles)

try:
    out = open("temp\\data.bin", "wb")
except IOError:
    print "<!> Cannot write output file;"
    exit(-1)

rom.seek(TM_START)
for x in xrange(TM_SIZE):
    out.write(tiles[(readWord(rom)&0xFF)])

###---
print "<i> Done;"
rom.close()
out.close()
